//
//  SearchTableViewCell.m
//  BondWatch
//
//  Created by Jason Yuan on 2/23/16.
//  Copyright © 2016 Jason Yuan. All rights reserved.
//

#import "SearchTableViewCell.h"
#import "AddToWatchListView.h"

@implementation SearchTableViewCell

@synthesize delegate;

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        issuerLabel = [[UILabel alloc] init];
        issuerLabel.textAlignment = NSTextAlignmentLeft;
        issuerLabel.font = ARIALBOLD10;
        
        tickerLabel = [[UILabel alloc] init];
        tickerLabel.textAlignment = NSTextAlignmentLeft;
        tickerLabel.font = ARIAL10;
        
        couponLabel = [[UILabel alloc] init];
        couponLabel.textAlignment = NSTextAlignmentRight;
        couponLabel.font = ARIAL10;
        
        maturaityLabel = [[UILabel alloc] init];
        maturaityLabel.textAlignment = NSTextAlignmentCenter;
        maturaityLabel.font = ARIAL10;
        
        ratingLabel = [[UILabel alloc] init];
        ratingLabel.textAlignment = NSTextAlignmentLeft;
        ratingLabel.font = ARIAL10;
        
        issAmtLabel = [[UILabel alloc] init];
        issAmtLabel.textAlignment = NSTextAlignmentRight;
        issAmtLabel.font = ARIAL10;
        
        starButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [starButton setBackgroundImage:[UIImage imageNamed:@"Star"] forState:UIControlStateNormal];
        [starButton addTarget:self action:@selector(starButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:issuerLabel];
        [self addSubview:tickerLabel];
        [self addSubview:couponLabel];
        [self addSubview:maturaityLabel];
        [self addSubview:ratingLabel];
        [self addSubview:issAmtLabel];
        [self addSubview:starButton];
    }
    return self;
}

- (void)setUIFrame {
    [issuerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(5);
        make.top.equalTo(self);
        make.bottom.equalTo(self);
        make.width.mas_equalTo(60);
    }];
    
    [tickerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(issuerLabel.mas_right);
        make.top.equalTo(self);
        make.bottom.equalTo(self);
        make.width.mas_equalTo(35);
    }];
    
    [couponLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(tickerLabel.mas_right);
        make.top.equalTo(self);
        make.bottom.equalTo(self);
        make.width.mas_equalTo(40);
    }];
    
    [maturaityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(couponLabel.mas_right);
        make.top.equalTo(self);
        make.bottom.equalTo(self);
        make.width.mas_equalTo(70);
    }];
    
    [ratingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(maturaityLabel.mas_right);
        make.top.equalTo(self);
        make.bottom.equalTo(self);
        make.width.mas_equalTo(60);
    }];
    
    [issAmtLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ratingLabel.mas_right);
        make.top.equalTo(self);
        make.bottom.equalTo(self);
        make.width.mas_equalTo(60);
    }];
    
    [starButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-8);
        make.top.equalTo(self).offset(8);
        make.size.mas_equalTo(CGSizeMake(16.0, 16.0));
    }];
}

- (void)setLabelText:(NSDictionary *)searchData {
    mySearchData = searchData;
    
    NSString *issuer = [searchData objectForKey:@"issuer"];
    issuerLabel.text = issuer;
    
    NSString *ticker = [searchData objectForKey:@"ticker"];
    tickerLabel.text = ticker;
    
    NSNumber *coupon = [searchData objectForKey:@"coupon"];
    couponLabel.text = [NSString stringWithFormat:@"%.3f", [coupon floatValue]];
    
    NSString *maturity = [searchData objectForKey:@"maturity"];
    maturaityLabel.text = maturity;
    
    NSString *rating = [searchData objectForKey:@"rating"];
    NSString *ratingSp = [searchData objectForKey:@"ratingSp"];
    ratingLabel.text = [NSString stringWithFormat:@"%@/%@", rating, ratingSp];
    
    NSNumber *issAmt = [searchData objectForKey:@"amountOuts"];
    issAmtLabel.text = [NSString stringWithFormat:@"%d", [issAmt intValue]/1000000];
}

- (void)starButtonPressed:(id)sender {
    [delegate addToWatchList:mySearchData];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
